package com.xiaolei.library.Net;

import android.content.Context;

import com.xiaolei.library.Net.RetrofitExt.Config;
import com.xiaolei.library.Net.RetrofitExt.common.IFailEvent;
import com.xiaolei.library.Net.RetrofitExt.factorys.GsonConverterFactory;
import com.xiaolei.library.Net.RetrofitExt.factorys.LiveDataCallAdapterFactory;
import com.xiaolei.library.Net.okhttputil.Cookie.CookieJar;
import com.xiaolei.library.Net.okhttputil.interceptor.CacheInterceptor;
import com.xiaolei.library.Net.okhttputil.interceptor.HeaderInterceptor;
import com.xiaolei.library.Utils.Log;

import java.net.Proxy;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * 设置网络的时候的配置
 */
public abstract class BaseNetConfig
{
    // 默认请求头的拦截器
    private final HeaderInterceptor headerInterceptor = new HeaderInterceptor();
    // Gson转换
    private final GsonConverterFactory gsonFactory = GsonConverterFactory.create();

    /**
     * 获取默认请求头的拦截器 用以修改默认请求头参数
     */
    public HeaderInterceptor getHeaderInterceptor()
    {
        return headerInterceptor;
    }

    /**
     * 获取Gson转换
     */
    public GsonConverterFactory getGsonFactory()
    {
        return gsonFactory;
    }

    /**
     * 代理
     */
    protected Proxy proxy()
    {
        return Proxy.NO_PROXY;
    }

    /**
     * 失败重连
     */
    protected boolean retryOnConnectionFailure()
    {
        return true;
    }

    /**
     * CookieJar
     */
    protected CookieJar cookieJar(Context context)
    {
        return new CookieJar(context);
    }

    /**
     * 连接超时
     *
     * @return 时间，单位秒
     */
    protected int connectTimeout()
    {
        return 30;
    }

    /**
     * 读取超时
     *
     * @return 时间，单位秒
     */
    protected int readTimeout()
    {
        return 30;
    }

    /**
     * 写出超时
     *
     * @return 时间，单位秒
     */
    protected int writeTimeout()
    {
        return 30;
    }

    /**
     * 日志打印拦截器
     */
    protected HttpLoggingInterceptor getLoggingInterceptor(OkHttpClient.Builder builder, Context context)
    {
        // 日志打印拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor((msg) -> Log.d("HttpRetrofit", msg));
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }


    /**
     * 在添加拦截器之前
     */
    protected void beforeAddInterceptor(OkHttpClient.Builder builder, Context context)
    {

    }

    /**
     * 添加拦截器
     */
    protected void onAddInterceptor(OkHttpClient.Builder builder, Context context)
    {
        // 默认头部的拦截器
        builder.addInterceptor(headerInterceptor);
        // 日志拦截器
        builder.addInterceptor(this.getLoggingInterceptor(builder, context));
    }

    /**
     * 在添加拦截器之后
     */
    protected void afterAddInterceptor(OkHttpClient.Builder builder, Context context)
    {
        // 网络缓存拦截器
        builder.addInterceptor(new CacheInterceptor(context, CacheInterceptor.Type.SQLITE));
    }

    /**
     * 在添加转换器之前
     */
    protected void beforeAddConverterFactory(Retrofit.Builder builder, Context context)
    {

    }

    /**
     * 添加转换器
     */
    protected void onAddConverterFactory(Retrofit.Builder builder, Context context)
    {
        builder.addConverterFactory(ScalarsConverterFactory.create()); //首先判断是否需要转换成字符串，简单类型
        builder.addConverterFactory(gsonFactory); //再将转换成bean
        builder.addCallAdapterFactory(new LiveDataCallAdapterFactory());
    }

    /**
     * 在添加转换器之后
     */
    protected void afterAddConverterFactory(Retrofit.Builder builder, Context context)
    {

    }

    /**
     * 设置失败的回调
     */
    public <T extends IFailEvent> void setFailedEvent(T failEvent)
    {
        Config.setFailedEvent(failEvent);
    }
}